<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>房间设置</title>
    <script type="text/javascript" src="../../script/aes.js"></script>
	<script type="text/javascript" charset="UTF-8" src="../../script/naes.js"></script>
	<script type="text/javascript" src="../../script/modeecb.js"></script>
    <script type="text/javascript" src="../../script/tmd5.js"></script>
    <script type="text/javascript" src="../../script/constants.js"></script>
    <script type="text/javascript" src="../../resource/jquery214.js"></script>
    <style>
    	body{
    	    margin: 0;	
    	    background:url(../../image/bj.png) repeat-x 0;
    	    background-size: 100%;
    	}
    	.title{
    	    height: 45px;
    	    background: #0483d3;
    	    line-height: 45px;
    	    font-size: 20px;
    	    margin-bottom: 20px;
    	}
    	
    	.input{
	    	float:left;
	    	width: 25%;
	    	height: 30px;
	    	padding: 5px;
	    	font-size: 16px;
	    	border:0;
	    	outline:none;
	    	border-radius: 5px;
    	}
    	.text{
	    	float: left;
	    	height: 30px;
	    	line-height: 30px;
	    	font-size: 18px;
	    	color: #666666;
	    	padding: 5px;
	    	text-align: center;
    	}
    </style>
</head>
<body>
<header id="statusBar">
  <div class="title">
    <div style="float: left;width: 30px;height: 30px;padding: 7.5px;" onclick="api.closeWin();">
    <img src="../../image/hotelManage/back.png"  style="width: 100%;"/>
    </div>
    <div style="float: left;color: #ffffff;">
    房间设置
    </div>
    <div style="float: right;color: #32CD32;margin-right: 20px;" onclick="submit();">
    完成
    </div>
  </div>
  </header>
  <div>
   <div style="">
    <div style="width: 30%;" class="text">房间号码：</div>
    <input type="number" class="input" placeholder="开始房间" id="start_num" onkeyup="value=value.replace(/[^\d]/g,'')"/>
    <div class="text" style="width: 5%;">-</div>
    <input type="number" class="input" placeholder="结束房间" id="end_num" onkeyup="value=value.replace(/[^\d]/g,'')"/>
   </div>
   <div style="height: 10px;clear: both;"></div>
   
   <div style="">
    <div style="width: 30%;" class="text">最大人数：</div>
    <input type="number" class="input" style="width: 60%" placeholder="请输入最大入住人数" id="max_num" onkeyup="value=value.replace(/[^\d]/g,'')"/>
   </div>
   <div style="height: 10px;clear: both;"></div>
   
   <div style="">
    <div style="width: 30%;" class="text">房间价格：</div>
    <input type="number" class="input" style="width: 60%" placeholder="请输入房间金价格" id="price" onkeyup="value=value.replace(/[^\d]/g,'')"/>
    <div style="height: 10px;clear: both;"></div>
   </div>
  </div>      
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript">
	apiready = function(){
	    if(api.systemType == "ios"){
		   document.getElementById("statusBar").style.background = "#298cb4";
		}
	    $api.fixStatusBar( $api.dom('header') );
	    
	    var uiId;
        var UILoading = api.require('UILoading');
		UILoading.flower({
		    center: {
		        x: api.winWidth/2.0,          
		        y: api.winHeight/2.0 
		    },
		    size: 30,
		    mask: "rgba(0,0,0,0.5)",
		    fixed: true
		}, function(ret) {
		    //alert(JSON.stringify(ret));
		    uiId = ret.id;
		});
	    var listData = "[";
	    var hotelId = $api.getStorage("hotelId");//"5101000562";
	    var callback = function(ret, err){
	        UILoading.closeFlower({
			    id: uiId
			});
	        if(ret.success == true || ret.success == "true"){
               var list = eval("("+ret.room+")");
               for(var i=0;i<list.length;i++){
                  listData += "{uid:'"+list[i].roomid+"',"
                           + "imgPath:'widget://image/hotelManage/room.png',"
                           + "title:'房间号："+list[i].roomnum+"',"
                           + "subTitle:'可住人数："+list[i].maxpeople+"  房间费用："+list[i].price+"元/天',";
                  if(list[i].roomstatus == "0"){
                    listData += "forbidden: false,"
                           + "remark: '空闲'},"
                  }else if(list[i].roomstatus == "1"){
                    listData += "forbidden: true,"
                           + "remark: '入住'},"
                  }else if(list[i].roomstatus == "2"){
                    listData += "forbidden: true,"
                           + "remark: '预定'},"
                  }          
               }
                listData = listData.substring(0,listData.length-1)+"]";
				var UIListView = api.require('UIListView');
				UIListView.open({
				    rect: {
				        x: 0,
				        y: 240,
				        w: api.winWidth,
				        h: api.frameHeight
				    },
				    data: eval("("+listData+")"),
				    rightBtns: [{
				        bgColor: '#FF6347',
				        activeBgColor: '',
				        width: 70,
				        title: '删除',
				        titleSize: 12,
				        titleColor: '#fff',
				        icon: '',
				        iconWidth: 20
				    }],
				    styles: {
				        borderColor: '#DEDEDE',
				        item: {
				            bgColor: '#ffffff',
				            activeBgColor: '#F5F5F5',
				            height: 70,
				            imgWidth: 40,
				            imgHeight: 40,
				            imgCorner: 4,
				            placeholderImg: '',
				            titleSize: 16.0,
				            titleColor: '#666666',
				            subTitleSize: 14.0,
				            subTitleColor: '#666666',
				            remarkColor: '#666666',
				            remarkSize: 16,
				            remarkIconWidth: 30
				        }
				    },
				    fixedOn: api.frameName
				}, function(ret, err) {
				  setTimeout(function(){
				    if (ret) {
				        if(ret.eventType == "clickRightBtn"){
					        UIListView.getDataByIndex({
							    index: ret.index
							}, function(ret, err) {
							    if (ret) {
							        if(ret.data.remark == "空闲"){
								        var callback = function(ret, err){
									        //console.log(JSON.stringify(ret));
									        if(ret){
									           if(true == ret.success || "true" == ret.success){
									              api.alert({msg:"删除成功"});
									              location.reload();
									           }
									        }else{
									           alert(JSON.stringify(err));
									        }
									    }
									    var content = {"hotelId":hotelId,"roomid":ret.data.uid};
									    apiAjax(JSON.stringify(content),url.delRoom,callback);
								    }else{
								        api.alert({msg: "房间正在使用中，无法删除"});
								        return;
								    }
							    } else {
							        alert(JSON.stringify(err));
							    }
							});
						}
				    } else {
				        alert(JSON.stringify(err));
				    }
				  },300); 
				});
            }
	    }
	    var content = {"hotelId":hotelId};
	    apiAjax(JSON.stringify(content),url.queryRoom,callback);
	};
	
	$('.input').bind('input propertychange', function() {
	          
	});
	
	//提交新增房间
	function submit(){
	   var uiId;
	   var sn = $("#start_num").val();
	   var en = $("#end_num").val();
	   var mn = $("#max_num").val();
	   var pr = $("#price").val();
	   var hotelId = $api.getStorage("hotelId");
	   var version = api.appVersion;
	   if(check(sn,en,mn,pr)){
	       var UILoading = api.require('UILoading');
		   UILoading.flower({
			    center: {
			        x: api.winWidth/2.0,          
			        y: api.winHeight/2.0 
			    },
			    size: 30,
			    mask: "rgba(0,0,0,0.5)",
			    fixed: true
		   }, function(ret) {
			    uiId = ret.id;
		   });
	       var callback = function(ret, err){
	            UILoading.closeFlower({
				    id: uiId
				});
		        //console.log(JSON.stringify(ret));
		        if(ret.success == true || ret.success == "true"){
		          api.alert({msg:"添加成功！"});
		          location.reload();
		        }else{
		          api.alert({msg:"添加失败！原因："+ret.msg});
		        }
		   }
		   var content = {"startNum":sn,"endNum":en,"max":mn,"price":pr,"hotelId":hotelId,"version":version};
		   apiAjax(JSON.stringify(content),url.setRoom,callback);
	   }
	}
	
	//检查数据
	function check(sn,en,mn,pr){
	   var flag = true;
	   if(!isNaN(sn)&&sn.length == 0){
	     alert("请输入数字类型的起始房间号");
	     $("#start_num").val("");
	     $("#start_num").select();
	     flag = false;
	     return;
	   }
	   if(!isNumber(en)&&en.length == 0){
	     alert("请输入数字类型的结束房间号");
	     $("#end_num").val("");
	     $("#end_num").select();
	     flag = false;
	     return;
	   }
	   if(!isNumber(mn)&&mn.length == 0){
	     alert("请输入数字类型的最大入住人数");
	     $("#max_num").val("");
	     $("#max_num").select();
	     flag = false;
	     return;
	   }
	   if(!isNumber(pr)&&pr.length == 0){
	     alert("请输入数字类型的房间价格");
	     $("#price").val("");
	     $("#price").select();
	     flag = false;
	     return;
	   }
	   if(parseInt(en)<parseInt(sn)){
	     alert("结束房间号必须小于起始房间号，请重新输入");
	     $("#end_num").val("");
	     $("#end_num").select();
	     flag = false;
	     return;
	   }
	   return flag;
	}
	
	function isNumber(value) {
	    var patrn = /^(-)?\d+(\.\d+)?$/;
	    if (patrn.exec(value) == null || value == "") {
	        return false
	    } else {
	        return true
	    }
	}
</script>
</html>